# @File    : ios.py
# @Project : JBL_Android&iOS
from common.yamlUtil import YamlUtil
from threading import Thread
from airtest.core.api import *
from poco.drivers.ios import iosPoco
from common.logUtil import info_i
from iOS.cancel_ota import CancelOta
from iOS.cycle_ota import CycleOta
from iOS.kill_ota import KillOta
from iOS.normal_ota import NormalOta


class IosUtil:

    conf = YamlUtil("data.yaml").read_yaml()
    driver = None
    poco = None

    def connect_ios(self):
        self.driver = connect_device("iOS:///127.0.0.1:8100")
        info_i("已连接到iOS手机")
        self.poco = iosPoco()

    # 升降级
    def promotion_demotion(self, num):
        def promotion():
            stop_app(f"{self.conf['app_ios']}")
            info_i("升降级测试开始...")
            sleep(2)
            info_i(f"压测次数为: {num}")
            start_app(f"{self.conf['app_ios']}")
            ota = NormalOta(self.driver, self.poco)
            ota.normal_ota(num)
            sleep(5)
            stop_app(f"{self.conf['app_ios']}")
            info_i("升降级测试结束...")

        promotion_thread = Thread(target=promotion)
        promotion_thread.start()

    # 取消升级
    def cancel_ota(self, num):
        def cancel():
            stop_app(f"{self.conf['app_ios']}")
            info_i("取消升级测试开始...")
            sleep(2)
            info_i(f"压测次数为: {num}")
            start_app(f"{self.conf['app_ios']}")
            ota = CancelOta(self.driver, self.poco)
            ota.cancel_ota(num)
            sleep(5)
            stop_app(f"{self.conf['app_ios']}")
            info_i("取消升级测试结束...")
        cancel_thread = Thread(target=cancel)
        cancel_thread.start()

    # 中断升级
    def kill_ota(self, num):
        def kill():
            stop_app(f"{self.conf['app_ios']}")
            info_i("中断升级测试开始...")
            sleep(2)
            info_i(f"压测次数为: {num}")
            start_app(f"{self.conf['app_ios']}")
            ota = KillOta(self.driver, self.poco)
            ota.kill_ota(num)
            sleep(5)
            stop_app(f"{self.conf['app_ios']}")
            info_i("中断升级测试结束...")

        kill_thread = Thread(target=kill)
        kill_thread.start()

    # 循环升级
    def cycle_upgrade(self, num):
        def cycle():
            stop_app(f"{self.conf['app_ios']}")
            info_i("循环升级测试开始...")
            sleep(2)
            info_i(f"压测次数为: {num}")
            start_app(f"{self.conf['app_ios']}")
            # 循环升级方法待编写
            ota = CycleOta(self.driver, self.poco)
            ota.cycle_ota(num)
            sleep(5)
            stop_app(f"{self.conf['app_ios']}")
            info_i("循环升级测试结束...")

        cycle_thread = Thread(target=cycle)
        cycle_thread.start()

    def read_process(self):
        def process():
            ota = NormalOta(self.driver, self.poco)
            ota.check_process()
        process_thread = Thread(target=process)
        process_thread.start()
